<template>
	<view class="container">
		<view class="img" style="text-align: center; height: 220px;">
			<image src="../../static/logo.png" mode="" style="width: 250px;height: 250px;scale: 0.4;"></image>
		</view>
		<uni-forms ref="form" :model="user" :rules="rules">
			<uni-forms-item label="用户名" name="name">

				<uni-easyinput v-model="user.name" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="password">

				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<button type="primary" @click="login">登录</button> <!-- 设置click事件方法  -->
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '请填写姓名'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请填写用户密码'
						}]
					}
				},
				user: {
					username: '',
					password: ''
				}
			}
		},
		methods: {
			login(e) {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/user/login',
					method: 'POST',
					data: this.user,
					success: res => {
						console.log(res);
						if (res.data.success === true) {
							// uni.showToast({
							// 	title: res.data.data.name + '登录成功',
							// 	icon: "success",
							// });
							uni.setStorageSync('userInfo', res.data.data)
							uni.showToast({
								title: '' + res.data.data.name,
								icon: 'none'
							});
							uni.switchTab({
								url: '../my/my'
							})
						} else {
							uni.showToast({
								title: '用户名或密码错误',
								icon: 'error'
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: '网络错误',
							icon: 'error'
						});
					},
					fail: () => {},
					complete: () => {}
				});
			}
		},
		onLoad: () => {
			uni.onTabBarMidButtonTap(function(e) {
				console.log(e)

			})
		}
	}
</script>

<style>
	.container {
		padding: 20rpx;
	}
</style>